<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-tree :data="syTypeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id" highlight-current @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px" class="top-left">
            <el-form-item label prop="jiancyj">
              <el-input v-model="queryParams.jiancyj" placeholder="请输入检测依据" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['sycs:bsJiancyj:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['sycs:bsJiancyj:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['sycs:bsJiancyj:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['sycs:bsJiancyj:export']">导出</el-button>
            </el-col>
            <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
          </el-row>

          <el-table border stripe v-loading="loading" :data="bsJiancyjList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="检测依据id" align="center" prop="jiancyjId" v-if="false" />
            <el-table-column label="试验类型" align="center" prop="syType" />
            <el-table-column label="检测依据" align="center" prop="jiancyj" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['sycs:bsJiancyj:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['sycs:bsJiancyj:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>
    <!-- 添加或修改检测依据设置对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属试验类型" prop="syTypeId">
          <el-cascader v-model="form.syTypeId" :options="syTypeList" :show-all-levels="false" :props="{ expandTrigger: 'hover', value: 'id', emitPath: false }" style="width: 100%;"></el-cascader>
        </el-form-item>
        <el-form-item label="检测依据" prop="jiancyj">
          <el-input v-model="form.jiancyj" placeholder="请输入检测依据" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsJiancyj, getBsJiancyj, delBsJiancyj, addBsJiancyj, updateBsJiancyj } from "@/api/sycs/bsJiancyj";
import { syTpyeTree } from "@/api/base/syType";

export default {
  name: "BsJiancyj",
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 试验类型列表
      syTypeList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测依据设置表格数据
      bsJiancyjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jiancyjId: undefined,
        jiancyj: undefined,
        syTypeId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jiancyjId: [{ required: true, message: "检测依据id不能为空", trigger: "blur" }],
        jiancyj: [{ required: true, message: "检测依据不能为空", trigger: "blur" }],
        syTypeId: [{ required: true, message: "试验类型id不能为空", trigger: "change" }]
      }
    };
  },
  created() {
    this.getSyTypeTreeList();
    // this.getList();
  },
  methods: {
    handleNodeClick(data) {
      this.form.syTypeId = undefined;
      this.queryParams.syTypeId = undefined;
      if (data.level == 2) {
        this.queryParams.syTypeId = data.id;
        this.form.syTypeId = data.id;
        this.handleQuery();
      }
    },
    /** 查询试验类型树 */
    getSyTypeTreeList() {
      let param = {
        level: 2
      };
      syTpyeTree(param).then((response) => {
        this.syTypeList = response.data;
      });
    },
    /** 查询检测依据设置列表 */
    getList() {
      this.loading = true;
      listBsJiancyj(this.queryParams).then((response) => {
        this.bsJiancyjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jiancyjId: undefined,
        jiancyj: undefined,
        syTypeId: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jiancyjId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.syTypeId = this.queryParams.syTypeId;
      this.open = true;
      this.title = "添加检测依据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const jiancyjId = row.jiancyjId || this.ids;
      getBsJiancyj(jiancyjId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改检测依据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.jiancyjId != null) {
            updateBsJiancyj(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsJiancyj(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jiancyjIds = row.jiancyjId || this.ids;
      this.$modal
        .confirm('是否确认删除检测依据设置编号为"' + jiancyjIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsJiancyj(jiancyjIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sycs/bsJiancyj/export",
        {
          ...this.queryParams
        },
        `bsJiancyj_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>


<style lang="scss" scoped>
  @import "~@/assets/styles/demo.scss";

  .table-add {
    text-align: center;
    line-height: 23px;
    padding: 4px;
    border: 1px solid #dfe6ec;
    border-top: 0px solid #dfe6ec;
  }
</style>
